/*!
    \file    main.c
*/

/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f30x.h"
#include <stdio.h>

#include "DevicesDelay.h"
#include "DevicesTime.h"
#include "DevicesUart.h"
#include "DevicesTimer.h"

#include "main.h"





void vUserSystemInit(void)
{
    /* 系统时基 */
    cTimer5Init();
}

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    /* enable IRC40K */
    rcu_osci_on(RCU_IRC40K);
    SystemCoreClockUpdate();

    /* 设定中断优先级分组 */
    nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);

    /* 开启复用时钟 */
    rcu_periph_clock_enable(RCU_AF);

    /* JTAG引脚复用成普通IO */
    gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);

    vDelayInit();
    vUart4Init();

    /* print out the clock frequency of system, AHB, APB1 and APB2 */
    printf("SystemCoreClock: %d\r\n", (int32_t)SystemCoreClock);
    printf("CK_SYS is %d\r\n", rcu_clock_freq_get(CK_SYS));
    printf("CK_AHB is %d\r\n", rcu_clock_freq_get(CK_AHB));
    printf("CK_APB1 is %d\r\n", rcu_clock_freq_get(CK_APB1));
    printf("CK_APB2 is %d\r\n\n", rcu_clock_freq_get(CK_APB2));

    if(RESET != rcu_flag_get(RCU_FLAG_LPRST))
        printf("低功耗复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_WWDGTRST))
        printf("窗口看门狗复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_FWDGTRST))
        printf("独立看门狗复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_SWRST))
        printf("软件复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_PORRST))
        printf("掉电/上电复位\r\n");
    if(RESET != rcu_flag_get(RCU_FLAG_EPRST))
        printf("NRST引脚复位\r\n");
    /* 清除复位标置 */
    rcu_all_reset_flag_clear();


    vUserSystemInit();


    while (1)
    {
        vDelayMs(1000.0f);

        printf("Time: %s, UTC: %d\r\n", pcStampToDateTimeStrings(cRealTimeUTCGet()), cRealTimeUTCGet());
    }
}

/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
    /* 等待发送寄存器发送完成，并且该串口已经在正常工作、已经使能发送功能 */
    while((RESET == usart_flag_get(UART4, USART_FLAG_TBE)) && ((USART_CTL0(UART4) & USART_CTL0_UEN) != 0) && ((USART_CTL0(UART4) & USART_TRANSMIT_ENABLE) != 0));

    /* Transmit Data */
    USART_DATA(UART4) = ch & 0xFF;

    return ch;
}
